package com.sl.au.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.Account;
import com.sl.au.entity.Division;
import com.sl.au.entity.Role;
import com.sl.au.entity.User;
import com.sl.au.entity.ViewForVisitorReward;
import com.sl.au.entity.VisitorReward;
import com.sl.au.entity.VisitorRewardProcessing;
import com.sl.au.service.RoleService;
import com.sl.au.service.UserService;
import com.sl.au.service.VisitorRewardProcessingService;
import com.sl.au.service.VisitorRewardService;
import com.sl.utils.MsgHelper;

@RestController
@RequestMapping("/ui/visitorRewardProcessing")
public class VisitorRewardProcessingController extends BaseController {

	@Autowired
	private VisitorRewardProcessingService visitorRewardProcessingService;
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index(ModelAndView modelAndView) {
		// PC端
		modelAndView = createLayoutView("visitorRewardProcessing/visitorRewardProcessingindex");
		return modelAndView;
	}
	
	@RequestMapping(value = "getpage", method = RequestMethod.GET)
	public Map<String, Object> getPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key=request.getParameter("key");
		Role role = roleService.getRepository().findByName("游客");
		
		List<ViewForVisitorReward> users =null;
		int count=0;
		users = visitorRewardProcessingService.getUserPage(start, length, key, role.getId(), null, null);
		count = visitorRewardProcessingService.getUserPageCount(key, role.getId());
		map.put("data", users);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	@RequestMapping(value = "saveView/{userId}",method = RequestMethod.GET)
	public ModelAndView saveView(ModelAndView modelAndView,@PathVariable String userId) {
		// PC端
		modelAndView = createLayoutView("visitorRewardProcessing/visitorRewardProcessingSave");
		ViewForVisitorReward user = visitorRewardProcessingService.getViewForVisitorRewardRepository().findOne(userId);
		modelAndView.addObject("user",user);
		String pictureAddress = "";
		pictureAddress = userService.getRepository().findOne(userId).getPictureAddress();
		modelAndView.addObject("pictureAddress",pictureAddress);
		return modelAndView;
	}
	
	@RequestMapping(value = "getProcessingPage", method = RequestMethod.GET)
	public Map<String, Object> getProcessingPage(HttpServletRequest request) {
		Map<String, Object> map = new HashMap<String, Object>();
		int start = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String key=request.getParameter("key");
		String userId=request.getParameter("userId");
		
		List<VisitorRewardProcessing> users =null;
		int count=0;
		users = visitorRewardProcessingService.getProcessingPage(start, length, key, userId, null, null);
		count = visitorRewardProcessingService.getProcessingPageCount(key, userId);
		map.put("data", users);
		map.put("draw", request.getParameter("draw"));
		map.put("recordsFiltered", count);
		map.put("recordsTotal", count);
		return map;
	}
	
	@RequestMapping(value = "save/{userId}", method = RequestMethod.POST)
	public MsgHelper save(VisitorRewardProcessing visitorRewardProcessing,@PathVariable("userId") String userId ) {
		MsgHelper msgHelper = MsgHelper.success();
		VisitorRewardProcessing newProcessing = null;
		if(!visitorRewardProcessing.getId().equals("")) {
			newProcessing = visitorRewardProcessingService.getRepository().findOne(visitorRewardProcessing.getId());
			newProcessing.setOperator(this.getMyAccount().getUser());
			newProcessing.setOperationTime(new Date());
			newProcessing.setRewardValue(visitorRewardProcessing.getRewardValue());
			newProcessing.setDetails(visitorRewardProcessing.getDetails());
//			newProcessing.setRedeemCode(newProcessing.getId());
		}else {
			User user = userService.getRepository().findOne(userId);
			newProcessing = new VisitorRewardProcessing();
			newProcessing.setOperator(this.getMyAccount().getUser());
			newProcessing.setOperationTime(new Date());
			newProcessing.setRewardValue(visitorRewardProcessing.getRewardValue());
			newProcessing.setDetails(visitorRewardProcessing.getDetails());
			newProcessing.setUser(user);
//			newProcessing.setRedeemCode(newProcessing.getId());
		}
		visitorRewardProcessingService.getRepository().save(newProcessing);
		msgHelper.setMsg("保存成功!");
		return msgHelper;
	}
}
